<template>
	<view>
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="content">
				<a-search :isOk="true" @change="searchChange" />
			</block>
		</cu-custom>
		<checkbox-group @change="checkboxChange">
			<a-scroll v-model="list" :request="get" ref="aScroll" :parm="{pageSize:30}">
				<template #item="{row:x,index}">
					<a-list :hideIcon='true' :value="{...x,index}" @click="back(index)">
						<template #right v-if="checkbox">
							<checkbox :value="index + ''" />
						</template>
					</a-list>
				</template>
			</a-scroll>
		</checkbox-group>
		<a-fixed v-if="checkbox" :height="70" :fixedStyle="{top:auto,bottom: '10px'}">
			<a-button style="margin: 0 20%;" @click="back()">确定</a-button>
		</a-fixed>
	</view>
</template>

<script>
	import {stringObject} from '@/a/utils/api.js';
	export default {
		//上拉触底
		onReachBottom() {
			this.$refs.aScroll.lower();
		},
		//下拉刷新
		onPullDownRefresh() {
			this.$refs.aScroll.get(true).then(res => {
				uni.stopPullDownRefresh();
			});
		},
		data() {
			return {
				searchText: "",
				parm:{},
				checkbox:false,
				list:[]
			}
		},
		onLoad(e) {
			this.parm = e;
		},
		methods: {
			checkboxChange({detail:{value}}){
				if(!this.checkbox){
					return ;
				}
				const {studentIds,studentName} = value.reduce((o,x)=>{
					x = this.list[x];
					o.studentIds.push(x.id);
					o.studentName.push(x.name);
					return o;
				},{studentIds:[],studentName:[]});
				this.parm = {
					...this.parm,
					studentIds:studentIds.toString(),
					studentName:studentName.toString(),
				}
			},
			back(index){
				if(!this.checkbox){
					const {id,name} = this.list[index];
					this.parm.studentIds = id;
					this.parm.studentName = name;
				}
				uni.redirectTo({
					url:`./proxy_leave${stringObject(this.parm)}`
				});
			},
			get(parm = {}) {
				return this.$http.post('/api/v1/student/selectStudents', {
					...parm,
					classId: this.$store.state.userInfo.classId,
					name: this.searchText
				}).then(result => {
					return {
						total: result.totalSize,
						data: result.results.map(x => {
							return {
								...x,
								img: this.$http.imgUrl + x.photo,
								contact: 123123,
								sonTitle: x.stuNumber,
								phone: x.telephone,
								img: this.$http.baseURL + x.photo
							}
						})
					}
				});
			},
			searchChange(e) {
				this.searchText = e;
				this.$refs.aScroll.get(true);
			}
		}
	}
</script>

<style lang="scss">

</style>
